// pages/personal_homepage/personal_homepage.js
var utils = require('../../../utils/util.js');
var httpUrl = getApp().globalData.httpUrl;
var codeName = getApp().globalData.codeName;
var userId = getApp().globalData.userId;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ishis: false, //用户回复是否弹出键盘
    isSubmit: false, //键盘输入触发“发送”按钮变色
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      modelid:options.modelid 
    })
    this.getlodeTap();
    
  },
  getlodeTap:function(e){
    var that = this;
    utils.http(httpUrl + "userclick", {
      code: codeName,
      userid: userId,
      modelid: that.data.modelid//模板id

    }, function (res) {
      // console.log(res);
      that.setData({
        image: res.catelist[0].user.image,
        name: res.catelist[0].user.name,
        releasenum: res.catelist[0].user.releasenum
      })
      var cateinfolist = [];
      for (var index in res.catelist) {
        if (res.catelist[index].content == undefined) {
          res.catelist[index].content = "";
        }
        //文字截取
        var sub = res.catelist[index].content.substring(0, 40);
        var subContent = sub + "...";
        that.setData({
          advlist: res.catelist[index].advlist
        })
        that.data.advlist.length = 3;
        // res.catelist[index].collect = !res.catelist[index].collect;
        var catePost = {
          advlist: that.data.advlist,
          subAdvlist: res.catelist[index].advlist, //截取后的图片
          city: res.catelist[index].city,
          code: res.catelist[index].code,
          collectby: res.catelist[index].collectby,
          content: res.catelist[index].content,
          subContent: subContent,//截取后的属性
          createTime: res.catelist[index].createTime,
          id: res.catelist[index].id,
          index: res.catelist[index].index,
          collect: res.catelist[index].collect,
          ischoiceness: res.catelist[index].ischoiceness,
          istop: res.catelist[index].istop,
          lookby: res.catelist[index].lookby,
          publishby: res.catelist[index].publishby,
          title: res.catelist[index].title,
          type: res.catelist[index].type,
          user: res.catelist[index].user,
          isstare: false,
          commentlist: res.catelist[index].commentlist,

        }
        cateinfolist.push(catePost);
      }
      that.setData({
        cateinfo: cateinfolist, // 列表区域     
      })
      console.log(that.data.cateinfo)
    })
  },
  //全文和收起
  noStareTap: function (e) {
    var index = e.currentTarget.dataset.index;
    this.data.cateinfo[index].isstare = !this.data.cateinfo[index].isstare;
    this.setData({
      cateinfo: this.data.cateinfo
    })
  },

  //收藏切换
  oNcollectbyTap: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    // that.data.cateinfo[index].collect = !that.data.cateinfo[index].collect;
    console.log(that.data.cateinfo[index].collect);
    var collectby = "";
    if (that.data.cateinfo[index].collect == false) {
      console.log(that.data.cateinfo[index].collect + "收藏");
      collectby = parseInt(that.data.cateinfo[index].collectby) + 1;
      that.data.cateinfo[index].collectby = collectby;
      utils.http(httpUrl + "manageCollect", {
        code: codeName,
        cateid: that.data.cateinfo[index].id,
        userid: userId
      }, function (res) {
        that.setData({
          cateinfo: that.data.cateinfo
        })
        that.getlodeTap();
      })
    } else {
      console.log(that.data.cateinfo[index].collect + "取消收藏");
      collectby = parseInt(that.data.cateinfo[index].collectby) - 1;
      that.data.cateinfo[index].collectby = collectby;
      utils.http(httpUrl + "manageCollect", {
        code: codeName,
        cateid: that.data.cateinfo[index].id,
        userid: userId
      }, function (res) {
        that.setData({
          cateinfo: that.data.cateinfo
        })
        that.getlodeTap();
      })
    }
  },


  //联系他
  onPhoneTap: function (e) {
    var phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone,
    });
  },

    //键盘输入是触发按钮
  oNbindinputTap: function (e) {
    var valueName = e.detail.value;
    if (valueName.length == 0) {
      this.data.isSubmit = false;
    } else {
      this.data.isSubmit = true;
    }
    this.setData({
      isSubmit: this.data.isSubmit,
      valueName: valueName
    })

  },

  //点击回复信息小图标
  oNinfonumTap: function (e) {
    var cateid = e.currentTarget.dataset.cateid;
    var nickname = e.currentTarget.dataset.nickname;
    var type = e.currentTarget.dataset.type;
    this.setData({
      ishis: true,
      cateid: cateid,
      nickname: nickname,
      type: type,
      valueName: "",
      username: "",
    })
  },

  //点击不同用户回复信息
  onchangeTap: function (e) {
    var that = this;
    var cateid = e.currentTarget.dataset.cateid;
    var parentusername = e.currentTarget.dataset.parentusername;
    var nickname = e.currentTarget.dataset.nickname;
    var parentid = e.currentTarget.dataset.parentid;
    var parentuserid = e.currentTarget.dataset.parentuserid;
    var username = e.currentTarget.dataset.username;   //要回复的username
    var type = e.currentTarget.dataset.type;
    var userid = e.currentTarget.dataset.userid;
    var id = e.currentTarget.dataset.id;
    if (parentuserid == undefined) {
      that.setData({
        parentid: id,
        parentuserid: userid,
      })
    } else {
      that.setData({
        parentid: parentid,
        parentuserid: parentuserid,
      })
    }

    this.setData({
      ishis: true,
      cateid: cateid,
      parentusername: parentusername,
      nickname: nickname,
      type: type,
      username: username,
      valueName: ""
    })
  },

  //提交回复
  bindsubmit: function (e) {
    var that = this;
    if (that.data.type == 1) {
      utils.http(httpUrl + "publishComment", {
        userid: userId, //	用户id
        cateid: that.data.cateid,//美食id
        code: codeName,//code值
        content: that.data.valueName, //	评论内容
        username: that.data.nickname  // 发布的用户的名字
      }, function (res) {
        console.log(res);
        that.getlodeTap();
      })
    } else {
      utils.http(httpUrl + "publishComment", {
        userid: userId, //	用户id
        cateid: that.data.cateid,//美食id
        code: codeName,//code值
        content: that.data.valueName, //	评论内容
        username: that.data.nickname,// 发布的用户的名字
        parentid: that.data.parentid,  //评论的ID
        parentuserid: that.data.parentuserid,
        parentusername: that.data.username
      }, function (res) {
        console.log(res);
        that.getlodeTap();
      })
    }





  },
  handletouchmove: function (e) {
    this.setData({
      ishis: false,
    })
  }

})